/**
 * @file commcn.c
 * @author Quan.Shen (ctrlshift12138@gmail.com)
 * @brief 
 * @version 0.1
 * @date 2025-07-22
 * 
 * @copyright Copyright (c) 2025
 * 
 */
#include "common.h"

/**
 * @brief 
 * 
 * @param key 
 * @return uint32_t 
 */
uint8_t SerialKeyPressed(uint8_t *key)
{
    if (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_RXNE) != RESET)
    {
        *key = (uint8_t)USART_ReceiveData(EVAL_COM1); // 从串口接收数据
        return 1; // 有数据可读
    }
    else
    {
        return 0; // 没有数据可读
    }
}

/**
 * @brief 获取按键输入
 * 
 * @return uint8_t 返回按键值
 */
uint8_t GetKey(void)
{
    uint8_t key = 0;

    while (1)
    {
        if (SerialKeyPressed(&key))
        {
            if (key == '\r' || key == '\n')
            {
                continue; // 忽略回车和换行符
            }
            else
            {
                return key; // 返回按键值
            }
        }
    }
}

/**
 * @brief 将整数转换为字符串
 * 
 * @param str 字符串缓冲区
 * @param int_num 整数值
 */
void Int2Str(uint8_t *str, uint32_t int_num)
{
    uint32_t i = 0;
    uint32_t div = 1000000000; // 10^9
    uint32_t j = 0;
    uint32_t status = 0;

    for (i = 0; i < 10; i++)
    {
        str[j++] = (uint8_t)(int_num / div) + '0';

        int_num %= div;
        div /= 10;
        if ((str[j - 1] == '0') &(status == 0))
        {
            j = 0; // 如果前导零，跳过
        }
        else
        {
            status++; // 有非零数字
        }
    }
}

/**
 * @brief 将字符串转换为整数
 * 
 * @param inputstr 输入字符串
 * @param intnum 输出整数指针
 * @return uint32_t 1 : 成功, 0 : 失败
 */
uint32_t Str2Int(uint8_t *inputstr, uint32_t *intnum)
{
  uint32_t i = 0, res = 0;
  uint32_t val = 0;

  if (inputstr[0] == '0' && (inputstr[1] == 'x' || inputstr[1] == 'X'))
  {
    if (inputstr[2] == '\0')
    {
      return 0;
    }
    for (i = 2; i < 11; i++)
    {
      if (inputstr[i] == '\0')
      {
        *intnum = val;
        /* return 1; */
        res = 1;
        break;
      }
      if (ISVALIDHEX(inputstr[i]))
      {
        val = (val << 4) + CONVERTHEX(inputstr[i]);
      }
      else
      {
        /* return 0, Invalid input */
        res = 0;
        break;
      }
    }
    /* over 8 digit hex --invalid */
    if (i >= 11)
    {
      res = 0;
    }
  }
  else /* max 10-digit decimal input */
  {
    for (i = 0;i < 11;i++)
    {
      if (inputstr[i] == '\0')
      {
        *intnum = val;
        /* return 1 */
        res = 1;
        break;
      }
      else if ((inputstr[i] == 'k' || inputstr[i] == 'K') && (i > 0))
      {
        val = val << 10;
        *intnum = val;
        res = 1;
        break;
      }
      else if ((inputstr[i] == 'm' || inputstr[i] == 'M') && (i > 0))
      {
        val = val << 20;
        *intnum = val;
        res = 1;
        break;
      }
      else if (ISVALIDDEC(inputstr[i]))
      {
        val = val * 10 + CONVERTDEC(inputstr[i]);
      }
      else
      {
        /* return 0, Invalid input */
        res = 0;
        break;
      }
    }
    /* Over 10 digit decimal --invalid */
    if (i >= 11)
    {
      res = 0;
    }
  }

  return res;
}

/**
 * @brief 发送字符到串口
 * 
 * @param ch 要发送的字符
 */
void SerialPutChar(uint8_t ch)
{
    USART_SendData(EVAL_COM1, ch); // 发送字符
    while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET); // 等待发送缓冲区空
}
